package com.zgc.view.common;

import com.fx.spring.context.ApplicationContextFX;
import com.fx.spring.util.SpringContextUtil;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXSnackbar;
import com.jfoenix.controls.JFXSnackbar.SnackbarEvent;
import com.jfoenix.effects.JFXDepthManager;
import com.jfoenix.svg.SVGGlyph;
import com.zgc.view.svg.SvgIconEnum;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class PopupCtrl {

    private static final Duration SHOW_TIME = Duration.seconds(5);

    private Label titleText;
    private TextArea messageArea;
    private final VBox vBox = new VBox(10);
    private final JFXSnackbar popup = new JFXSnackbar();
    private final SnackbarEvent snackbarEvent = new SnackbarEvent(vBox, SHOW_TIME);
    private final ApplicationContextFX contextFX = SpringContextUtil.getBean(ApplicationContextFX.class);


    private void initialize() {
        if (messageArea == null) {
            messageArea = new TextArea();
            messageArea.setEditable(false);
            messageArea.setStyle("-fx-background-color: transparent");
            vBox.getChildren().addAll(titleText = new Label());
            JFXDepthManager.pop(vBox);
            vBox.setStyle("-fx-background-color: #ffffff");
            vBox.setAlignment(Pos.CENTER);
            vBox.setPadding(new Insets(10));
            vBox.setPrefSize(500, 200);
            VBox.setVgrow(messageArea, Priority.ALWAYS);
        }
        if (!vBox.getChildren().contains(messageArea)) {
            vBox.getChildren().add(messageArea);
        }
    }


    /**
     * 一次触发只有一个弹窗，等第一个弹窗自动隐藏后才可以重新点击触发
     *
     * @param snackbarContainer 在那个Pane上展示
     * @param title             标题
     * @param messages          message
     */
    public void enqueueOne(Pane snackbarContainer, String title, String[] messages) {
        enqueue(snackbarContainer, title, messages);
        popup.enqueue(snackbarEvent);
    }

    /**
     * 一次只有一个弹窗，不会等待以一个弹窗，根点击的次数以队列形式展示
     *
     * @param snackbarContainer 在那个Pane上展示
     * @param title             标题
     * @param messages          message
     */
    public void enqueueMore(Pane snackbarContainer, String title, String... messages) {
        enqueue(snackbarContainer, title, messages);
        popup.enqueue(new SnackbarEvent(vBox, SHOW_TIME));
    }

    /**
     * 在根节点上显示
     *
     * @param title    标题
     * @param messages message
     */
    public void enqueueRoot(String title, String... messages) {
        Parent root = contextFX.getPrimaryStage().getScene().getRoot();
        if (root instanceof Pane) {
            enqueue((Pane) root, title, messages);
            popup.enqueue(new SnackbarEvent(vBox, SHOW_TIME));
        }
    }

    private void enqueue(Pane snackbarContainer, String title, String... messages) {
        initialize();
        defaultPopup();
        titleText.setText(title);
        if (!StringUtils.isEmpty(messages)) {
            for (String message : messages) {
                messageArea.appendText(message + "\n");
            }
        } else {
            vBox.getChildren().remove(messageArea);
        }
        if (popup.getPopupContainer() != null) {
            popup.unregisterSnackbarContainer(snackbarContainer);
        }
        popup.registerSnackbarContainer(snackbarContainer);
    }


    private void defaultPopup() {
        titleText.setText(null);
        messageArea.clear();
    }

    private HBox topBar() {
        SVGGlyph close = SvgIconEnum.ICON_CLOSE.init(Color.valueOf("#4D66CC"), 12, 12);
        JFXButton closePopup = new JFXButton();
        closePopup.getStyleClass().add("common-border-test");
        closePopup.setGraphic(close);
        Region parent = new Region();
        HBox hBox = new HBox(titleText = new Label(), parent, close);
        hBox.setAlignment(Pos.CENTER_LEFT);
        HBox.setHgrow(parent, Priority.ALWAYS);
        close.setOnMouseClicked(event -> {
            popup.close();
        });
        return hBox;
    }
}
